<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>医院栏目管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/admin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">

                <div style="padding-bottom: 10px;padding-top: 20px;margin-left: 10px">
                    {if (buttonAuth('cate/add'))}
                    <button class="layui-btn layuiadmin-btn-admin" data-type="add" id="addCate">
                        <i class="layui-icon">&#xe654;</i> 添加顶级栏目
                    </button>
                    {/if}
                    <blockquote class="layui-elem-quote" style="width: 300px;float: right;border-left: 5px solid #FFB800">
                        提示： 红色 <i class="layui-icon layui-icon-right" style="color: #FF5722"></i> 表示下面有子栏目，可点击打开
                    </blockquote>
                </div>
                <div class="layui-card-body">
                    <table id="demo" lay-filter="test"></table>
                    <table class="layui-table">
                        <thead>
                        <tr>
                            <th>ID</th>
                            <th>栏目名称</th>
                            <th>父栏目</th>
                            <th>栏目拼音</th>
                            <th>添加时间</th>
                            <th>排序</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        {foreach name="tree" item="vo"}
                        <tr>
                            <td>{$vo['id']}</td>
                            <td>{$vo['classname']}</td>
                            <td>{$vo['bclassid']}</td>
                            <td>{$vo['enname']}</td>
                            <td>{$vo['add_time']}</td>
                            <td>{$vo['sort']}</td>
                            <td>
                                <div class="layui-btn-group">
                                    {if (buttonAuth('cate/add'))}
                                    <button type="button" class="layui-btn layui-btn-sm" onclick="addSub(this)" data-pid="{$vo['id']}" data-name="{$vo['title']}">
                                        <i class="layui-icon">&#xe654;</i> 添加子栏目
                                    </button>
                                    {/if}
                                    {if (buttonAuth('cate/edit'))}
                                    <button type="button" class="layui-btn layui-btn-sm layui-bg-blue" onclick="editCate(this)" data-id="{$vo['id']}" data-pid="{$vo['pid']}">
                                        <i class="layui-icon">&#xe642;</i> 编辑栏目
                                    </button>
                                    {/if}
                                    {if (buttonAuth('cate/delete'))}
                                    <button type="button" class="layui-btn layui-btn-sm layui-bg-red" onclick="delCate(this)" data-id="{$vo['id']}">
                                        <i class="layui-icon">&#xe640;</i> 删除栏目
                                    </button>
                                    {/if}
                                </div>
                            </td>
                        </tr>
                        {if !empty($vo['children'])}
                        {foreach name="$vo['children']" item="v"}
                        <tr>
                            <td>{$v['id']}</td>
                            <td> &nbsp;&nbsp;&nbsp;&nbsp; <i class="layui-icon layui-icon-right" onclick="showSub(this)" data-flag="0" data-id="{$v['id']}" style="cursor: pointer;{if !empty($v['children'])}color:#FF5722{/if}"></i> {$v['classname']}</td>
                            <td>{$v['bclassid']}</td>
                            <td>{$v['enname']}</td>
                            <td>{$v['add_time']}</td>
                            <td>{$v['sort']}</td>
                            <td>
                                <div class="layui-btn-group">
                                    {if (buttonAuth('cate/add'))}
                                    <button type="button" class="layui-btn layui-btn-sm" onclick="addSub(this)" data-pid="{$v['id']}" data-name="{$v['classname']}">
                                        <i class="layui-icon">&#xe654;</i> 添加子栏目
                                    </button>
                                    {/if}
                                    {if (buttonAuth('cate/edit'))}
                                    <button type="button" class="layui-btn layui-btn-sm layui-bg-blue" onclick="editCate(this)" data-id="{$v['id']}" data-pid="{$v['bclassid']}">
                                        <i class="layui-icon">&#xe642;</i> 编辑栏目
                                    </button>
                                    {/if}
                                    {if (buttonAuth('cate/delete'))}
                                    <button type="button" class="layui-btn layui-btn-sm layui-bg-red" onclick="delCate(this)" data-id="{$v['id']}">
                                        <i class="layui-icon">&#xe640;</i> 删除栏目
                                    </button>
                                    {/if}
                                </div>
                            </td>
                        </tr>
                        {if !empty($v['children'])}
                        {foreach name="$v['children']" item="vl"}
                        <tr style="display: none" class="tree-{$vl['pid']}">
                            <td>{$vl['id']}</td>
                            <td> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  <i class="layui-icon layui-icon-right"></i> {$vl['classname']}</td>
                            <td>{$v['bclassid']}</td>
                            <td>{$v['enname']}</td>
                            <td>{$vl['add_time']}</td>
                            <td>{$vl['sort']}</td>
                            <td>
                                <div class="layui-btn-group">
                                    {if (buttonAuth('cate/edit'))}
                                    <button type="button" class="layui-btn layui-btn-sm layui-bg-blue" onclick="editCate(this)" data-id="{$vl['id']}" data-pid="{$vl['pid']}">
                                        <i class="layui-icon">&#xe642;</i> 编辑栏目
                                    </button>
                                    {/if}
                                    {if (buttonAuth('cate/delete'))}
                                    <button type="button" class="layui-btn layui-btn-sm layui-bg-red" onclick="delCate(this)" data-id="{$vl['id']}">
                                        <i class="layui-icon">&#xe640;</i> 删除栏目
                                    </button>
                                    {/if}
                                </div>
                            </td>
                        </tr>
                        {/foreach}
                        {/if}
                        {/foreach}
                        {/if}
                        {/foreach}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="/static/layui/layui.js"></script>
<script src="/static/common/js/layTool.js"></script>
<script src="/static/common/js/jquery.min.js"></script>
<script>
    function renderTable() {
        window.location.reload();
    }

    $(function () {
        $("#addCate").click(function () {
            layTool.open('{:url("cate/add", ["pid" => 0, "pname" => "顶级栏目"])}', '添加顶级栏目', '40%', '60%');
        });
    });

    function addSub(obj) {
        var pid = $(obj).attr('data-pid');
        var pname = $(obj).attr('data-name');
        layTool.open('/admin/cate/add/pid/' + pid + '/pname/' + pname, '添加字栏目', '40%', '50%');
    }

    function editCate(obj) {
        var id = $(obj).attr('data-id');
        var pid = $(obj).attr('data-pid');
        layTool.open('/admin/cate/edit/pid/' + pid + '/id/' + id, '编辑栏目', '40%', '50%');
    }

    function delCate(obj) {
        var id = $(obj).attr('data-id');
        layui.use('layer', function () {
            var layer = layui.layer;
            layer.ready(function () {
                var index = layer.confirm('您确定要删除该栏目？', {
                    title: '友情提示',
                    icon: 3,
                    btn: ['确定', '取消']
                }, function(){
                    $.getJSON('{:url("cate/delete")}', {id: id}, function (res) {
                        if (0 == res.code) {
                            layTool.msg(res.msg);

                            setTimeout(function () {
                                renderTable();
                            }, 500);
                        } else {

                            layTool.alert(res.msg, '', 2);
                        }
                    });
                }, function(){

                });
            });
        });
    }

    function showSub(obj) {
        var flag = $(obj).attr('data-flag');
        if (0 == flag) {
            $(".tree-" + $(obj).attr('data-id')).show();
            $(obj).attr('data-flag', 1);
        } else {
            $(".tree-" + $(obj).attr('data-id')).hide();
            $(obj).attr('data-flag', 0);
        }
    }
</script>
</body>
</html>